package PinDuoDuo;

import org.junit.After;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.Scanner;

public class Solution3 {
    static int number;
    static int count;
    static int N;
    static int compare;
    static int i,j;
    public static void main(String args[]){
        Scanner scanner1;
        Scanner scanner = new Scanner(System.in);
        scanner1 = new Scanner(scanner.nextLine());

        N=scanner1.nextInt();
        compare=scanner1.nextInt();

        int[][] map = new int[N+1][N+1];
        i=0;j=0;
        number=-1;
        count=0;
        while (scanner.hasNextLine()){
            scanner1 = new Scanner(scanner.nextLine());
            while (scanner1.hasNext()){

                map[i][j]=scanner1.nextInt();
                j++;
            }
            map[i][j]=-1;
            i++;
            j=0;
        }
        if(map[compare][0]==-1){
            System.out.println(-1);
            return;
        }

        for (int k = 0; k < N; k++) {
            if(k==compare)
                continue;
            if(find(map[compare],k)>=0)//找到了，说明是朋友关系
                continue;
            int z=0;
            for (int l = 0; l < N; l++) {
                if(map[k][l]==-1)
                    break;
                if(find(map[compare],map[k][l])>=0)
                    z++;
            }
            if(z>count){
                count=z;
                number=
                        k;
            }
        }
        System.out.println(number);
    }
    public static int find(int[] a,int k){
        for (int l = 0; l < a.length; l++) {
            if(a[l]==k)
                return 1;
        }
        return -1;
    }
    /**
     * 遍历
     * 1、如果已是朋友关系,跳过
     * 2、计算共同朋友
     * 3、由于返回序号小的，如果相等就不更新
     */

    @Test
    public void test1(){
        String data = "5 0\n" +
                "1 2 3 4\n" +
                "0 4\n" +
                "0 1 3\n" +
                "0 2 4\n" +
                "0 3";
        System.setIn(new ByteArrayInputStream(data.getBytes()));
        String[] args={};
        main(args);
    }
    @After
    public void after(){

    }
}
